﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TimKiem
{
    public partial class frmHangHoaNhapKho : Form
    {
        #region khởi tạo
        bool checkClose = false;
        bool check = false;
        public static Entities.ChiTietHoaDonNhap chitiethoadonnhap;
        Entities.ChiTietHoaDonNhap[] hienthi;
        public frmHangHoaNhapKho()
        {
            InitializeComponent();
        }
        public frmHangHoaNhapKho(Entities.ChiTietHoaDonNhap ct)
        {
            InitializeComponent();
            dgvHienThi.DataSource = HienThi(ct);
            fix();
            hienthi = (Entities.ChiTietHoaDonNhap[])dgvHienThi.DataSource;
        }

        public Entities.ChiTietHoaDonNhap[] HienThi(Entities.ChiTietHoaDonNhap ct)
        {
            List<Entities.ChiTietHoaDonNhap> array = new List<Entities.ChiTietHoaDonNhap>();
            Entities.ChiTietHoaDonNhap[] temp = Common.StaticVariable.InterfaceObject.Select_ChiTietHoaDonNhap(ct);
            foreach (Entities.ChiTietHoaDonNhap item in temp)
            {
                if (item.Deleted == false)
                {
                    if (item.SoLuongConLai != 0)
                    {
                        array.Add(item);
                    }
                }
            }
            Entities.ChiTietHoaDonNhap[] chitiethoadonnhap = (Entities.ChiTietHoaDonNhap[])array.ToArray();
            return chitiethoadonnhap;
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.ChiTietHoaDonNhap.MaHangHoa].Visible = true;
            dgvHienThi.Columns[Common.Constants.ChiTietHoaDonNhap.TenHangHoa].Visible = true;
            dgvHienThi.Columns[Common.Constants.ChiTietHoaDonNhap.SoLuongConLai].Visible = true;

            dgvHienThi.Columns[Common.Constants.ChiTietHoaDonNhap.MaHangHoa].HeaderText = "Mã Hàng Hóa";
            dgvHienThi.Columns[Common.Constants.ChiTietHoaDonNhap.TenHangHoa].HeaderText = "Tên Hàng Hóa";
            dgvHienThi.Columns[Common.Constants.ChiTietHoaDonNhap.SoLuongConLai].HeaderText = "Số Lượng Còn Lại";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Tìm Kiếm
        public void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.ChiTietHoaDonNhap[])hienthi.Where(p => p.TenHangHoa.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
        }
        #endregion

        #region Chọn
        void Chon()
        {
            try
            {
                if (i < 0)
                    return;
                if (dgvHienThi.RowCount == 0)
                    return;
                chitiethoadonnhap = ((Entities.ChiTietHoaDonNhap[])dgvHienThi.DataSource)[i];
                if (chitiethoadonnhap == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                checkClose = true;
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Event

        private void frmHangHoaNhapKho_Load(object sender, EventArgs e)
        {
            btnChon.Click += new EventHandler(btnChon_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            btnTim.Click += new EventHandler(btnTim_Click);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            this.FormClosing += new FormClosingEventHandler(frmKhachHang_FormClosing);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Chon();
            }
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        void frmKhachHang_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Chon();
        }
        int i = 0;
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnChon_Click(object sender, EventArgs e)
        {
            Chon();
        }
        #endregion

    }
}
